package com.quhaodian.plug.data.plugs.nonepush;


import com.quhaodian.plug.data.entity.PluginConfig;
import com.quhaodian.plug.data.plugs.BasePlugController;
import com.quhaodian.plug.data.service.PluginConfigService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;

@Controller
@RequestMapping("/admin/push_plugin/nonepush")
public class NonePushController extends BasePlugController{


    @Resource(name = "nonePushPlugin")
    private NonePushPlugin nonePushPlugin;


    @Resource(name = "pluginConfigServiceImpl")
    private PluginConfigService pluginConfigService;


    /**
     * 安装
     */
    @RequestMapping(value = "/install")
    public String install( RedirectAttributes redirectAttributes) {
        if (!nonePushPlugin.getIsInstalled()) {
            PluginConfig pluginConfig = new PluginConfig();
            pluginConfig.setPluginId(nonePushPlugin.getId());
            pluginConfig.setIsEnabled(false);
            pluginConfigService.save(pluginConfig);
        }
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);

        return "redirect:/admin/push_plugin/list.htm";
    }

    /**
     * 卸载
     */
    @RequestMapping(value = "/uninstall")
    public String uninstall(RedirectAttributes redirectAttributes) {
        if (nonePushPlugin.getIsInstalled()) {
            PluginConfig pluginConfig = nonePushPlugin.getPluginConfig();
            pluginConfigService.deleteById(pluginConfig.getId());
        }
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:/admin/push_plugin/list.htm";
    }

    /**
     * 设置
     */
    @RequestMapping(value = "/setting", method = RequestMethod.GET)
    public String setting(ModelMap model) {
        PluginConfig pluginConfig = nonePushPlugin.getPluginConfig();
        model.addAttribute("pluginConfig", pluginConfig);
        return "/admin/push_plugin/setting_nonepush";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update( @RequestParam(defaultValue = "false") Boolean isEnabled, Integer order, RedirectAttributes redirectAttributes) {
        PluginConfig pluginConfig = nonePushPlugin.getPluginConfig();
        pluginConfig.setIsEnabled(isEnabled);
        pluginConfig.setSortNum(order);
        pluginConfigService.update(pluginConfig);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:/admin/push_plugin/list.htm";
    }

}
